package ca.concordia.soen6461.wichf.dao.mem;

import java.util.LinkedList;
import java.util.List;

import ca.concordia.soen6461.wichf.dao.PatientCartDao;
import ca.concordia.soen6461.wichf.entity.PatientCart;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.User;

public class PatientCartMemDao implements PatientCartDao {

	public PatientCartMemDao() {
	}

	@Override
	public PatientCart get(User user) {
		PatientCart cart = new PatientCart();
		cart.setPatient(user);
		cart = DB.patientCartTable.get(cart);
		if (cart != null) {
			List<ScheduledEvent> appointments = new LinkedList<ScheduledEvent>();
			for (ScheduledEvent appointment : cart.getAppointments()) {
				ScheduledEvent a = DB.scheduledEventTable.get(appointment);
				if (a != null) {
					appointments.add(a);
				}
			}
			cart.getAppointments().clear();
			cart.getAppointments().addAll(appointments);
		}
		return cart;
	}

	@Override
	public void save(PatientCart cart) {
		DB.patientCartTable.save(cart);
	}
	
}
